package cn.wolfcode.web.controller;


import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.Question;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;
    @Autowired
    private IQuestionService questionService;

    @RequiredPermission(name="试卷页面",expremission="exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "exam/list";
    }

    @RequiredPermission(name="试卷新增/编辑",expremission="exam:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Exam exam, Model model) {
        //先把你穿过来的exam放进数据库中
        examService.save(exam);
        Long id = exam.getId();

        //把你传来的id放进共享域中,然后在list页面
        model.addAttribute("id",id);

        //获取所有的单选题的集合因为他的typeid=1
        List<Question> questions=questionService.selectByTypeId(1L);
        //获取多选题的集合因为他的typeid=2
        List<Question> questions2=questionService.selectByTypeId(2L);
        //获取判断题的集合因为他的typeid都是3
        List<Question> questions3=questionService.selectByTypeId(3L);

        //获取你前端页面传过来的单选题数
        Integer danxuan = exam.getDanxuan();

        //获取你前端页面传过来的多选题数
        Integer duoxuan = exam.getDuoxuan();

        //获取你前端页面川谷弄来的判断题述
        Integer judge = exam.getJudge();

        //RandomUtil.randomEle 随机获得单选列表中的元素
        for(int i=0;i<danxuan;i++){
             examService.insertRelation(id,RandomUtil.randomEle(questions),exam.getScore(),i+1);
        }
        //RandomUtil.randomEle 随机获得多选列表中的元素
        for(int i=0;i<duoxuan;i++){
            examService.insertRelation(id,RandomUtil.randomEle(questions2),exam.getScore(),i+1);
        }
        //RandomUtil.randomEle 随机获得判断列表中的元素
        for(int i=0;i<judge;i++){
            examService.insertRelation(id,RandomUtil.randomEle(questions3),exam.getScore(),i+1);
        }

        //return "redirect:/exam/input";
        return "redirect:/exam/browse?id="+exam.getId();
       // return "redirect:/exam/browse";sadas
    }


    @RequestMapping("/browse")
    @RequiredPermission(name="试卷跳转",expremission="exam:browse")
    public String browse(Long id,Model model){
        List<Exam> examList= examService.selectByExam(id);
        model.addAttribute("exam",examList);
        model.addAttribute("exams",examService.get(id));
        List<Exam> answers=examService.selectAnswer(id);
        model.addAttribute("answers",answers);
        List<Exam> trueAnswer=examService.selectTrueAnswer(id);
        model.addAttribute("trueAnswer",trueAnswer);
        return "exam/input";
    }




  /*  @RequiredPermission(name="试卷新增/编辑",expremission="exam:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("exam",examService.get(id));
        }
        return "exam/input";
    }*/

}
